package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.SearchAdapter;
import com.lc.luzhengtang.adapter.SearchHistoryAdapter;
import com.lc.luzhengtang.adapter.SearchStaticListAdapter2;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.SearchBean;
import com.lc.luzhengtang.bean.SearchKeywordBean;
import com.lc.luzhengtang.conn.ConnGoodsHistory;
import com.lc.luzhengtang.conn.ConnGoodsSearch;
import com.lc.luzhengtang.divider.SimpleDividerItemDecoration;
import com.lc.luzhengtang.layoutmanager.FullyGridLayoutManager;
import com.lc.luzhengtang.view.ClearEditText;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/3.
 */

public class SearchHistoryActivity extends BaseActivity {

    @BindView(R.id.et_search)
    ClearEditText etSearch;
    @BindView(R.id.text_cancel)
    TextView textCancel;
    @BindView(R.id.rv_searchhistory)
    RecyclerView rvSearchhistory;
    List<SearchKeywordBean.DataBean> mDatas;
    SearchHistoryAdapter searchHistoryAdapter;
    @BindView(R.id.layout_search)
    LinearLayout layoutSearch;
    /*@BindView(R.id.recycleview_searchlist)
    RecyclerView recycleviewSearchlist;*/
    List<SearchBean.DataBeanX.DataBean> dataBeanList = new ArrayList<>();
    SearchStaticListAdapter2 searchStaticListAdapter2;
    @BindView(R.id.layout_title_bar)
    LinearLayout layoutTitleBar;
    private PopupWindow popLeft;
    private View layoutLeft;
    private ListView menulistLeft;
    List<String> list0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        mDatas = new ArrayList<>();
        list0 = new ArrayList<>();
//        recycleviewSearchlist.setLayoutManager(new LinearLayoutManager(this));
        getDatqa();
//        getSearchData();
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getSearchData();
//               getSearchData();
               /* Intent intent = new Intent();
                intent.putExtra("searchcode", etSearch.getText().toString().trim());
                intent.setClass(SearchHistoryActivity.this, SearchListActivity.class);
                startActivity(intent);*/


            }
        });

    }

    public void getSearchData() {

        new ConnGoodsSearch(etSearch.getText().toString().trim(), "1", BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<SearchBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, SearchBean s) throws Exception {
                super.onSuccess(toast, type, o, s);

                dataBeanList.clear();
                dataBeanList = s.getData().getData();

                searchStaticListAdapter2 = new SearchStaticListAdapter2(dataBeanList);
                if (dataBeanList != null) {
                    searchStaticListAdapter2.setNewData(dataBeanList);
                }
                for (int i = 0; i < dataBeanList.size(); i++) {
                    list0.add(dataBeanList.get(i).getTitle());
                }

                if (etSearch.getText().toString().equals("")) {
                    popLeft.dismiss();
                } else {

                    if (popLeft != null && popLeft.isShowing()) {
                        popLeft.dismiss();
                    } else {
                        if (dataBeanList.isEmpty()) {
                            popLeft.dismiss();
                        }else {
                            layoutLeft = getLayoutInflater().inflate(
                                    R.layout.pop_menulist1, null);
                            menulistLeft = (ListView) layoutLeft
                                    .findViewById(R.id.menulist);
//                            menulistLeft.setAdapter(new ArrayAdapter<String>(SearchHistoryActivity.this, R.layout.spinner_list_item, list0));

                            menulistLeft.setAdapter(new SearchAdapter(SearchHistoryActivity.this, dataBeanList));


                            menulistLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent();
                                    intent.putExtra("searchcode", list0.get(position));
                                    intent.setClass(SearchHistoryActivity.this, SearchListActivity.class);
                                    startActivity(intent);

                                    popLeft.dismiss();
                                }
                            });
                            popLeft = new PopupWindow(layoutLeft, rvSearchhistory.getWidth(),
                                    RelativeLayout.LayoutParams.WRAP_CONTENT);
                            popLeft.setAnimationStyle(R.style.PopupAnimation);
                            popLeft.setBackgroundDrawable(new BitmapDrawable());
                            popLeft.setFocusable(true);
                            popLeft.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                            popLeft.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                            popLeft.showAsDropDown(layoutTitleBar, 0, 0);
                        }


                    }


                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

            }
        }

        ).execute(SearchHistoryActivity.this, false);


    }


    public void getDatqa() {


        new ConnGoodsHistory(new AsyCallBack<SearchKeywordBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, SearchKeywordBean searchKeywordBean) throws Exception {
                super.onSuccess(toast, type, o, searchKeywordBean);

                mDatas.addAll(searchKeywordBean.getData());
                initAdapter();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }


        ).execute(SearchHistoryActivity.this, false);


    }


    private void initAdapter() {
        searchHistoryAdapter = new SearchHistoryAdapter(mDatas);
        rvSearchhistory.setLayoutManager(new FullyGridLayoutManager(SearchHistoryActivity.this, 3));
        searchHistoryAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//        pullToRefreshAdapter.setAutoLoadMoreSize(3);
        Drawable drawable = ContextCompat.getDrawable(SearchHistoryActivity.this, R.drawable.line_divider2);
        rvSearchhistory.addItemDecoration(new SimpleDividerItemDecoration(SearchHistoryActivity.this, drawable, 30));
        rvSearchhistory.setAdapter(searchHistoryAdapter);

    }


    @OnClick({R.id.et_search, R.id.text_cancel, R.id.layout_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_search:
//                getDatqa();

              /*  if (TextUtils.isEmpty(etSearch.getText().toString().trim())) {
//                    Toast.makeText(SearchHistoryActivity.this, Integer.toString(position), Toast.LENGTH_LONG).show();
                   LovelyToast.makeText(SearchHistoryActivity.this, "请输入搜索的信息", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
//                    startActivity(new Intent(SearchHistoryActivity.this, SearchListActivity.class));
                    Intent intent = new Intent();
                    intent.putExtra("searchcode", etSearch.getText().toString().trim());
                    intent.setClass(SearchHistoryActivity.this, SearchListActivity.class);
                    startActivity(intent);
                }*/
                break;
            case R.id.text_cancel:
                finish();
                break;

            case R.id.layout_search:
              /*  Intent intent = new Intent();
                intent.putExtra("searchcode", etSearch.getText().toString().trim());
                intent.setClass(SearchHistoryActivity.this, SearchListActivity.class);
                startActivity(intent);*/
//                startActivity(new Intent(SearchHistoryActivity.this, SearchListActivity.class));
                break;
        }
    }

}
